package com.itheima02.dao;

import com.itheima02.domain.Student;
import com.itheima02.utils.JdbcUtilsDemo;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@SuppressWarnings("all")
public class StudentDaoDemo implements DaoDemo {
    ArrayList<Student> list = new ArrayList<>();
    Student stu = null;
    Connection con = JdbcUtilsDemo.getConnection();
    Statement sta = null;
    ResultSet res = null;

    @Override
    public ArrayList<Student> findAll() {
        try {
            sta = con.createStatement();
            String sql = "select * from student";
            res = sta.executeQuery(sql);
            while (res.next()) {
                Integer sid = res.getInt("sid");
                String name = res.getString("name");
                Integer age = res.getInt("age");
                Date birthday = res.getDate("birthday");
                stu = new Student(sid, name, age, birthday);
                list.add(stu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtilsDemo.close(res, sta, con);
        }
        return list;
    }

    @Override
    public Student findById(Integer id) {
        try {
            sta = con.createStatement();
            String sql = "select * from student where sid=" + id + "";
            res = sta.executeQuery(sql);
            if (res.next()) {
                Integer sid = res.getInt("sid");
                String name = res.getString("name");
                Integer age = res.getInt("age");
                Date birthday = res.getDate("birthday");
                stu = new Student(sid, name, age, birthday);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtilsDemo.close(res, sta, con);
        }
        return stu;
    }

    @Override
    public int insert(Student stu) {
        int res = 0;
        try {
            sta = con.createStatement();
            Date date = stu.getBirthday();
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(date);
            String sql = "insert into student value('" + stu.getSid() + "','" + stu.getName() + "','" + stu.getAge() + "','" + birthday + "')";
            res = sta.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtilsDemo.close(null, sta, con);
        }
        return res;
    }

    @Override
    public int update(Student stu, Integer id) {
        int res = 0;
        try {
            sta = con.createStatement();
            Date date = new Date();
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(date);
            String sql = "update student set sid='" + id + "',name='" + stu.getName() + "',age='" + stu.getAge() + "',birthday='" + birthday + "' where sid=" + stu.getSid() + "";
            res = sta.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtilsDemo.close(null, sta, con);
        }
        return res;
    }

    @Override
    public int delete(Integer id) {
        int res = 0;
        try {
            sta = con.createStatement();
            String sql = "delete from student where sid=" + id + "";
            res = sta.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtilsDemo.close(null, sta, con);
        }
        return res;
    }
}
